package netective.api.analyzer;

import java.util.Calendar;
import java.sql.Timestamp;

import netective.dao.DatabaseHelper;
import netective.gui.charts.ChartGrain;

public class PacketAnalyzer extends Thread{
	DatabaseHelper dh;
	private static ChartGrain g=ChartGrain.Minute;
	
	public PacketAnalyzer() {
		dh = new DatabaseHelper();
	}
	
/*	public int getPacketsCount(PacketType packetType) {
		return dh.getPacketsCount(packetType, null, null);
	}
	
	public int getPacketsCount(PacketType packetType, Date start, Date finish) {
		return dh.getPacketsCount(packetType, start, finish);
	}*/
	
	@Override
	public void run() 
	{
		while(true)
		{
			try 
			{
				Thread.sleep(60 * 1000); //analyze chart data every 60s
			} 
			catch (InterruptedException e) 
			{
				e.printStackTrace();
			}
			analyze();
		}
	}
	
	@SuppressWarnings("deprecation")
	private void analyze()
	{
		Calendar c=Calendar.getInstance();
		dh.setlastAnalyze(new Timestamp(c.get(Calendar.YEAR)-1900,c.get(Calendar.MONTH),c.get(Calendar.DAY_OF_MONTH),c.get(Calendar.HOUR_OF_DAY),c.get(Calendar.MINUTE),0,0));
		System.out.println(new Timestamp(c.get(Calendar.YEAR)-1900,c.get(Calendar.MONTH),c.get(Calendar.DAY_OF_MONTH),c.get(Calendar.HOUR_OF_DAY),c.get(Calendar.MINUTE),0,0));
		classesOfPacketsAnalyze();
		dirrectionOfPacketsAnalyze();
		downloadUploadAnalyze();
		downloadUploadAverageAnalyze();
		PortAnalyze();
		adressesAnalyze();
		dh.deleteTemp();
		
	}
	
	private void classesOfPacketsAnalyze()
	{
		String query;
		query=dh.getPacketsCountsQuery(PacketType.TCPPacket, g);
		dh.insert("classesOfPackets",query);
		query=dh.getPacketsCountsQuery(PacketType.UDPPacket, g);
		dh.insert("classesOfPackets",query);
		query=dh.getPacketsCountsQuery(PacketType.ICMPPacket, g);
		dh.insert("classesOfPackets",query);
		query=dh.getPacketsCountsQuery(PacketType.ARPPacket, g);
		dh.insert("classesOfPackets",query);
		query=dh.getPacketsCountsQuery(PacketType.IPPacket, g);
		dh.insert("classesOfPackets",query);
	}
	
	private void dirrectionOfPacketsAnalyze()
	{
		String query;
		query=dh.getPacketsCountsQuery(PacketDirection.In, g);
		dh.insert("dirrectionOfPackets",query);
		query=dh.getPacketsCountsQuery(PacketDirection.Out, g);
		dh.insert("dirrectionOfPackets",query);
		
	}


	private void downloadUploadAnalyze()
	{
		String query;
		query=dh.getDownUpQuery(PacketDirection.In, g);
		dh.insert("downloadUpload",query);
		query=dh.getDownUpQuery(PacketDirection.Out, g);
		dh.insert("downloadUpload",query);
		
	}
	
	private void downloadUploadAverageAnalyze()
	{
		String query;
		query=dh.getDownUpAvgQuery(PacketDirection.In, g);
		dh.insert("downloadUploadAvg",query);
		query=dh.getDownUpAvgQuery(PacketDirection.Out, g);
		dh.insert("downloadUploadAvg",query);
		
	}
	
	private void PortAnalyze()
	{
		String query;
		query=dh.getPortQuery(PacketDirection.In, g);
		dh.insert("inPort",query);
		query=dh.getPortQuery(PacketDirection.Out, g);
		dh.insert("outPort",query);
		query=dh.otherPortQuery(g);
		dh.insert("otherPort", query);
	}
	
	private void adressesAnalyze()
	{
		String query=dh.otherAdressesQuery(g);		
		dh.insert("otherAdresses",query);
	}
}
